<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>一维数组 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/16.6e68160b.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础语法</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/a5bdcf/" class="sidebar-link">计算机导论与环境配置</a></li><li><a href="/pages/32e9a2/" class="sidebar-link">Java开发入门与数据类型</a></li><li><a href="/pages/0ccc48/" class="sidebar-link">变量和运算符</a></li><li><a href="/pages/67fd43/" class="sidebar-link">选择语句和循环语句</a></li><li><a href="/pages/92a804/" aria-current="page" class="active sidebar-link">一维数组</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/92a804/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_1-1-学习内容" class="sidebar-link">1.1 学习内容</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_1-2-学习目标" class="sidebar-link">1.2 学习目标</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/92a804/#二、数组" class="sidebar-link">二、数组</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_2-1-数组概念和定义-重点" class="sidebar-link">2.1 数组概念和定义（重点）</a></li><li class="sidebar-sub-header level4"><a href="/pages/92a804/#_2-1-1-什么是数组-理解" class="sidebar-link">2.1.1 什么是数组(理解）</a></li><li class="sidebar-sub-header level4"><a href="/pages/92a804/#_2-1-2-定义语法-重点" class="sidebar-link">2.1.2 定义语法（重点）</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_2-2-数组的初始化-重点" class="sidebar-link">2.2 数组的初始化（重点）</a></li><li class="sidebar-sub-header level4"><a href="/pages/92a804/#_2-2-1-静态初始化-重点" class="sidebar-link">2.2.1 静态初始化（重点）</a></li><li class="sidebar-sub-header level4"><a href="/pages/92a804/#_2-2-2-动态初始化-重点" class="sidebar-link">2.2.2 动态初始化（重点）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/92a804/#三、数组基本操作" class="sidebar-link">三、数组基本操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_3-1-基本操作-重点" class="sidebar-link">3.1 基本操作（重点）</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_3-2-数组遍历操作-重点" class="sidebar-link">3.2 数组遍历操作 （重点）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/92a804/#四、数组应用" class="sidebar-link">四、数组应用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_4-1-查找元素" class="sidebar-link">4.1 查找元素</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_4-2-求最大值和最小值" class="sidebar-link">4.2 求最大值和最小值</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_4-3-统计和累加求和" class="sidebar-link">4.3 统计和累加求和</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_4-4-格式化输出数组元素" class="sidebar-link">4.4 格式化输出数组元素</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_4-5-答辩评分" class="sidebar-link">4.5 答辩评分</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/92a804/#五、数组内存图" class="sidebar-link">五、数组内存图</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_5-1-jvm内存模型-了解-面试题" class="sidebar-link">5.1 JVM内存模型（了解）面试题</a></li><li class="sidebar-sub-header level3"><a href="/pages/92a804/#_5-2-静态初始化内存分析-理解即可" class="sidebar-link">5.2 静态初始化内存分析（理解即可）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/92a804/#六、小结与作业" class="sidebar-link">六、小结与作业</a></li></ul></li><li><a href="/pages/0840c0/" class="sidebar-link">方法定义与使用</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95" title="分类" data-v-06225672>基础语法</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-02-28</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">一维数组<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="一维数组"><a href="#一维数组" class="header-anchor">#</a> 一维数组</h1> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <h3 id="_1-1-学习内容"><a href="#_1-1-学习内容" class="header-anchor">#</a> 1.1 学习内容</h3> <ul><li>数组的定义</li> <li>数组的初始化</li> <li>数组的基本操作和遍历操作</li> <li>数组综合应用练习</li> <li>JVM的内存模型和数组内存图</li></ul> <h3 id="_1-2-学习目标"><a href="#_1-2-学习目标" class="header-anchor">#</a> 1.2 学习目标</h3> <ul><li>必须掌握数组定义的语法</li> <li>必须掌握数组的静态初始化和动态初始化</li> <li>必须掌握数组的基本操作和遍历操作</li> <li>必须掌握数组的应用</li> <li>JVM内存模型
<ul><li>了解JVM的内存模型中的栈区、堆区、方法区</li> <li>数组内存图</li></ul></li></ul> <h2 id="二、数组"><a href="#二、数组" class="header-anchor">#</a> 二、数组</h2> <h3 id="_2-1-数组概念和定义-重点"><a href="#_2-1-数组概念和定义-重点" class="header-anchor">#</a> 2.1 数组概念和定义（重点）</h3> <p>为什么要学习数组？</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 问题1:定义一个变量，存储一个学生的年龄？</span>
<span class="token keyword">int</span> age <span class="token operator">=</span> <span class="token number">17</span><span class="token punctuation">;</span>

<span class="token comment">// 问题2：如果班级有100人，那么是否需要100个变量呢？</span>
<span class="token keyword">int</span> age1 <span class="token operator">=</span> <span class="token number">18</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> age2 <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> age3 <span class="token operator">=</span> <span class="token number">17</span><span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>​		在之前我们可以通过一个变量表示一个学生的年龄，如果现在需要表示全班100个人的年龄岂不是需要定义100个变量来分别表示。这样的操作太麻烦了，为了解决这种问题，Java就提供了数组(Array)（ 多个数据组合在一起统一管理 ）</p> <h4 id="_2-1-1-什么是数组-理解"><a href="#_2-1-1-什么是数组-理解" class="header-anchor">#</a> 2.1.1 什么是数组(理解）</h4> <p>简单地说，<strong>数组</strong>是一个容器，用来存储相同类型数据的容器</p> <p>所谓<strong>数组</strong>(<strong>Array</strong>)，是一种容器，里面存储相同类型数据，这些数据按一定编号排列存储在这个容器中。</p> <ul><li>这些类型相同的多个数据需要一个名字以便统一管理它们，这个名字称为<strong>数组名</strong></li> <li>组成数组的各个数据称为数组的<strong>元素</strong>( element )，数组中元素的个数称为<strong>数组的长度</strong>(length)。</li> <li>数组每个元素都用一个唯一的编号称为<strong>下标或索引</strong>(index)。表示元素存放的位置，索引从0开始，步长是1，有点像Excel表格的行号。</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/QAqGInesiPjECDf.png" alt=""></strong></p> <p><strong>数组的特点：</strong></p> <p>1、数组的长度一旦确定就不能修改</p> <p>2、创建数组时会在内存中开辟一整块连续的空间，每个空间都已被编号</p> <p>3、存取元素的速度极快，因为可以使用下标，直接定位到任意一个元素。</p> <h4 id="_2-1-2-定义语法-重点"><a href="#_2-1-2-定义语法-重点" class="header-anchor">#</a> 2.1.2 定义语法（重点）</h4> <p>回忆定义变量的语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>数据类型 变量名<span class="token punctuation">;</span> 
<span class="token keyword">int</span> age<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>数组是一种新的数据类型，可以用于定义变量(也就是开辟内存空间)</p> <p>数组的定义语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>数组元素类型<span class="token punctuation">[</span><span class="token punctuation">]</span> 数组名<span class="token punctuation">;</span>
例如<span class="token operator">:</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> ages<span class="token punctuation">;</span>	
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>从定义中可以理解到：</p> <ol><li><p>可以把int[]看成是一种数据类型——int元素的数组类型（<strong>简称：int数组类型</strong>）。</p></li> <li><p>int[] 数组可以看出，该数组中的元素类型是int类型的。</p></li> <li><p>String[] 数组可以看出，该数组中的元素是String类型的。</p></li></ol> <h3 id="_2-2-数组的初始化-重点"><a href="#_2-2-数组的初始化-重点" class="header-anchor">#</a> 2.2 数组的初始化（重点）</h3> <p><strong>数组在定义后，必须初始化才能使用</strong>。所谓初始化，就是在<strong>内存</strong>中给数组分配存储空间，并为每一个元素赋上初始值，有两种方式：</p> <ul><li><p>静态初始化；</p></li> <li><p>动态初始化；</p></li></ul> <p><strong>数组的长度是固定的</strong>，无论以哪种，一旦初始化完成，数组的长度（元素的个数）就固定了，不能改变，除非重新对它初始化。</p> <p>那么静态初始化和动态初始化如何选用呢？</p> <ul><li>如果我们事先知道元素是什么，选用静态初始化；</li> <li>事先不知道元素是什么(但知道需要多少个的空间)，选用动态初始化。</li></ul> <h4 id="_2-2-1-静态初始化-重点"><a href="#_2-2-1-静态初始化-重点" class="header-anchor">#</a> 2.2.1 静态初始化（重点）</h4> <p>程序员直接为每一个数组元素设置初始值，而数组的长度由元素个数决定。</p> <p>初始化语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>数组元素类型<span class="token punctuation">[</span><span class="token punctuation">]</span>  数组名 <span class="token operator">=</span> <span class="token keyword">new</span> 数组元素类型<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span>元素<span class="token number">1</span><span class="token punctuation">,</span>元素<span class="token number">2</span><span class="token punctuation">,</span>元素<span class="token number">3</span><span class="token punctuation">,</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">// 例如:</span>
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span>  <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// 简单写法(语法糖),定义和初始化必须同时写出来</span>
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="_2-2-2-动态初始化-重点"><a href="#_2-2-2-动态初始化-重点" class="header-anchor">#</a> 2.2.2 动态初始化（重点）</h4> <p>程序员只设置数组元素个数，开发者可以提前把数组内存空间申请好，然后再程序运行过程中添值。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>语法<span class="token operator">:</span>
数组元素类型<span class="token punctuation">[</span><span class="token punctuation">]</span>  数组名 <span class="token operator">=</span> <span class="token keyword">new</span> 数组元素类型<span class="token punctuation">[</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// 例如</span>
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span>  <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>不同数据类型默认的初始值：</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/9RcuHCXx4BaZdyS.png" alt=""></em></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>		<span class="token keyword">int</span>类型数组，每一个元素的初始值为<span class="token number">0</span>
<span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">double</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>	<span class="token keyword">double</span>类型数组，每一个元素初始值为<span class="token number">0.0</span>
<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>	<span class="token class-name">String</span>类型数组，每一个元素的初始值为<span class="token keyword">null</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>注意 :</strong></p> <p>不能同时指定元素值和数组长度，反例如下:</p> <p>int[] nums = new int[5]{1,3,5,7,9};</p> <h2 id="三、数组基本操作"><a href="#三、数组基本操作" class="header-anchor">#</a> 三、数组基本操作</h2> <h3 id="_3-1-基本操作-重点"><a href="#_3-1-基本操作-重点" class="header-anchor">#</a> 3.1 基本操作（重点）</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li><strong>获取数组长度</strong>。获取数组元素的个数</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>语法：
<span class="token keyword">int</span> size <span class="token operator">=</span>  数组名<span class="token punctuation">.</span>length<span class="token punctuation">;</span>		

例如<span class="token operator">:</span>nums<span class="token punctuation">.</span>length 可以获取nums数组元素的个数
<span class="token keyword">int</span> size <span class="token operator">=</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span>  <span class="token comment">// 输出结果4</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li><strong>获取元素值</strong></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>语法：
元素类型 变量名 <span class="token operator">=</span> 数组名<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span>
数组的索引从<span class="token number">0</span>开始，最大索引值是数组长度<span class="token operator">-</span><span class="token number">1</span>，那么索引范围是 <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> 数组名<span class="token punctuation">.</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>

<span class="token comment">// 获取第一个元素</span>
<span class="token keyword">int</span>	ele1 <span class="token operator">=</span> nums<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>		<span class="token comment">// 输出1</span>
<span class="token comment">// 获取第二个元素：</span>
<span class="token keyword">int</span>	ele2 <span class="token operator">=</span> nums<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>		<span class="token comment">// 输出3</span>
<span class="token comment">// 获取第四个元素：</span>
<span class="token keyword">int</span>	ele4 <span class="token operator">=</span> nums<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>		<span class="token comment">// 输出7</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li><strong>设置元素值</strong>。把一个新值存入数组对应索引位置的空间中，原来的值会被覆盖。</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>语法：
数组名<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">=</span> 值<span class="token punctuation">;</span>

<span class="token comment">// 设置第二个元素值为30</span>
nums<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span>  <span class="token number">30</span><span class="token punctuation">;</span>

<span class="token comment">// 获取第二个元素</span>
<span class="token keyword">int</span>  ele2 <span class="token operator">=</span> nums<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>	<span class="token comment">// 输出30</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><strong>常见的异常</strong></p> <p><strong>1、ArrayIndexOutOfBoundsException：数组的索引越界异常</strong></p> <p><strong>异常原因：操作的数组的索引不在[0，数组名.length-1]范围内</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> ele <span class="token operator">=</span> nums<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;ele = &quot;</span> <span class="token operator">+</span> ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>2、NullPointerException：空指针异常（空引用异常）</p> <p>异常原因：操作了一个尚未初始化或者没有分配内存空间的数组</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> len <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_3-2-数组遍历操作-重点"><a href="#_3-2-数组遍历操作-重点" class="header-anchor">#</a> 3.2 数组遍历操作 （重点）</h3> <p>遍历数组（获取出数组中每一个元素）</p> <p>获取第一个元素：int	ele1 = nums[0];		输出1</p> <p>获取第二个元素：int	ele2 = nums[1];		输出30</p> <p>获取第三个元素：int	ele3 = nums[2];		输出5</p> <p>获取第四个元素：int	ele4 = nums[3];		输出7</p> <p>观察发现：获取元素的过程是一个重复性的操作，遍历的次数是数组元素的个数，每一次获取元素只有索引在变化，范围是[0 , 数组名.length - 1]。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span> ele <span class="token operator">=</span> nums<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//index依次是 0、1、2、3</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>使用for-each（增强for循环）操作数组</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">for</span><span class="token punctuation">(</span>数组元素类型 变量<span class="token operator">:</span> 数组<span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">// 使用变量</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>使用for-each操作数组更简单，因为可以不关心索引，其底层原理依然是上述的for循环操作数组。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>需求：给一个数组 int[] nums = new int[] { 1, 3, 5, 7 }，打印输出数组每个索引和索引位置的元素</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo7</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token string">&quot;=&gt;&quot;</span> <span class="token operator">+</span> ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 技巧: arr.fori</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="四、数组应用"><a href="#四、数组应用" class="header-anchor">#</a> 四、数组应用</h2> <p>应用前提：提前定义好数组</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span>  nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">,</span><span class="token number">44</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-1-查找元素"><a href="#_4-1-查找元素" class="header-anchor">#</a> 4.1 查找元素</h3> <p><strong>需求：找出数组中元素22第一次出现的索引位置,并打印输出该索引位置</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo8</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">,</span><span class="token number">44</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">66</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> target <span class="token operator">=</span> <span class="token number">220</span><span class="token punctuation">;</span>        <span class="token comment">// 要查找的目标元素</span>

        <span class="token comment">// step 1: 依次取出数组的每个元素</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> item <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment">// step 2: 用取出的元素和要找的目标元素22进行比较</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span> item <span class="token operator">==</span> target <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// step 3: 如果元素和22相等，停止查找并打印输出，否则继续查找</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> 
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="_4-2-求最大值和最小值"><a href="#_4-2-求最大值和最小值" class="header-anchor">#</a> 4.2 求最大值和最小值</h3> <p>需求2：求出int类型数组中最大元素值</p> <p>思路：</p> <ol><li>暂定第一个元素为最大，用变量max存起来</li> <li>用max和后面的元素比较，如果元素比max大，则把这个元素赋值给max</li> <li>遍历完成后，max存的数值就是最大值</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo9</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">55</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
		<span class="token comment">// step 1: 定义一个变量max 用于存储最大值，假设第一个元素最大</span>
        <span class="token keyword">int</span> max <span class="token operator">=</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

        <span class="token comment">// step 2: 从1号位置开始，依次取出每个元素</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment">// step 3: 取出来的元素和max比较，如果取出来的元素大于max，把大的元素存入max中，直到全部比较完成</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span> ele <span class="token operator">&gt;</span> max <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                max <span class="token operator">=</span> ele<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// step 4：打印max</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;max = &quot;</span> <span class="token operator">+</span> max<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="_4-3-统计和累加求和"><a href="#_4-3-统计和累加求和" class="header-anchor">#</a> 4.3 统计和累加求和</h3> <p>需求：某部门5名员工的销售额分别是：16、128、36、6、100，完成以下需求：</p> <ul><li>计算出他们部门的总销售额</li> <li>达标销售额为100，统计完成销售额的人数</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo10</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">128</span><span class="token punctuation">,</span><span class="token number">36</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token comment">// (1)计算出他们部门的总销售额</span>
        <span class="token keyword">int</span> total <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token comment">// 1 遍历数组，取出每个元素</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 2 定义变量total，累加每个元素</span>
            total <span class="token operator">+=</span> ele<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;total = &quot;</span> <span class="token operator">+</span> total<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// (2)达标销售额为100，统计完成销售额的人数</span>
        <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>ele <span class="token operator">&gt;=</span> <span class="token number">100</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                count<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;count = &quot;</span> <span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_4-4-格式化输出数组元素"><a href="#_4-4-格式化输出数组元素" class="header-anchor">#</a> 4.4 格式化输出数组元素</h3> <p>需求：按照某种格式打印数组元素，数组元素放在方括号[]中,相邻元素使用逗号分隔开。如上述数组打印出来，效果如：[11, 22, 33, 44, 22, 55]</p> <p>思路：</p> <ol><li>定义一个String类型的变量，准备用来拼接指定格式的字符串</li> <li>将 [ 拼接到字符串中</li> <li>通过循环遍历的方式，将每个元素拼接到字符串中，每个元素后面拼接一个逗号，</li> <li>如果是最后一个元素，不是拼接逗号，而是拼接一个 ]</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo11</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">55</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token comment">// str表示结果字符串,先拼一个[符号</span>
		<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;[&quot;</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token comment">// 把每一个元素拼接在str后面 </span>
			str <span class="token operator">=</span> str <span class="token operator">+</span> ele<span class="token punctuation">;</span>
			<span class="token comment">// 如果是最后一个元素,拼接]</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">==</span> nums<span class="token punctuation">.</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				str <span class="token operator">=</span> str <span class="token operator">+</span> <span class="token string">&quot;]&quot;</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
				<span class="token comment">// 如果不是最后一个元素拼接,</span>
				str <span class="token operator">=</span> str <span class="token operator">+</span> <span class="token string">&quot;, &quot;</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_4-5-答辩评分"><a href="#_4-5-答辩评分" class="header-anchor">#</a> 4.5 答辩评分</h3> <p>需求5：</p> <p>在毕业答辩中，有6个导师为答辩的学生打分（控制台输入），分数值为0-100之间(含0和100)，</p> <p>学生答辩的最后得分为: 去掉一个最高分和一个最低分后的4个导师评分的平均值</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/*
分析：
1 键盘录入6位导师的成绩
2 求和得到总成绩
3 求最大值
4 求最小值
5 ( 总成绩 - (最高分 + 最低分) ) / 4.0
*/</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo12</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token comment">// 1.键盘录入6个导师的打分</span>
        <span class="token class-name">Scanner</span> sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入6个导师的打分:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;请输入第”+ (i + 1) +“位导师的打分:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> score <span class="token operator">=</span> sc<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> score<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token comment">// 3 累加求和</span>
        <span class="token keyword">int</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> ele <span class="token operator">:</span> scoreArr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            sum <span class="token operator">+=</span> ele<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token comment">// 最大值</span>
        <span class="token keyword">int</span> max <span class="token operator">=</span> scoreArr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> scoreArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> scoreArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>ele <span class="token operator">&gt;</span> max<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                max <span class="token operator">=</span> ele<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 最小值</span>
        <span class="token keyword">int</span> min <span class="token operator">=</span> scoreArr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> scoreArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> ele <span class="token operator">=</span> scoreArr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>ele <span class="token operator">&lt;</span> min<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                min <span class="token operator">=</span> ele<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 4 (累加求和-最大值-最小值) / 4.0</span>
        <span class="token keyword">double</span> avg <span class="token operator">=</span> <span class="token punctuation">(</span>sum <span class="token operator">-</span> max <span class="token operator">-</span> min<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1.0</span> <span class="token operator">/</span> <span class="token number">4</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;最终成绩 = &quot;</span> <span class="token operator">+</span> avg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br></div></div><h2 id="五、数组内存图"><a href="#五、数组内存图" class="header-anchor">#</a> 五、数组内存图</h2> <h3 id="_5-1-jvm内存模型-了解-面试题"><a href="#_5-1-jvm内存模型-了解-面试题" class="header-anchor">#</a> 5.1 JVM内存模型（了解）面试题</h3> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/JtjzCyPRm4lKxUe.png" alt=""></em></p> <p>JVM内存划分，人为的根据不同内存空间的存储特点以及存储的数据。</p> <ul><li><p>程序计数器：当前线程所执行的字节码的行号指示器。</p></li> <li><p>本地方法栈：为虚拟机使用的native方法服务。</p></li> <li><p><strong>方法区：存储已被虚拟机加载的类 (xx.class ) 信息</strong></p></li> <li><p>**Java虚拟机栈：**简称栈区（stack），每当调用一个方法时，在栈区分配方法执行时的内存，存放了当前方法的变量，当方法调用完毕，该方法的内存就被销毁了。一句话：<strong>java方法执行时，在栈区执行</strong></p></li> <li><p>**Java堆：**简称堆区(heap), <strong>所有的对象以及 <code>数组</code> 都要在堆上分配内存。每次使用new关键字，就表示在堆内存中开辟一块新的存储空间。</strong></p></li> <li><p>GC(Garbage Collection)，垃圾回收器。Java的自动垃圾回收机制可以简单理解为，不需要程序员手动的去控制内存的释放。当JVM内存资源不够用的时候，就会自动地去清理堆中无用对象（没有被引用到的对象）所占用的内存空间。</p></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// jvm 内存模型</span>
<span class="token comment">/**
* 1 方法区：用于存储字节码(xx.class)信息，也即类信息
* 2 栈区：方法执行时在栈区分配内存空间。一句话:java方法执行时，在栈区执行。
* 3 堆区：数组的内存都分配在该区。该区变量有一个标志——new操作符，表示在堆内存中开辟一块新的存储空间。
*/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_5-2-静态初始化内存分析-理解即可"><a href="#_5-2-静态初始化内存分析-理解即可" class="header-anchor">#</a> 5.2 静态初始化内存分析（理解即可）</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// 定义并初始化数组</span>
		<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;数组长度=&quot;</span> <span class="token operator">+</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">// 重新初始化数组</span>
		nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;数组长度=&quot;</span> <span class="token operator">+</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>对上述代码做内存分析</p> <p>int[] nums = new int[] { 1, 3, 5, 7 };</p> <p>这一行代码可以分成三步：</p> <ul><li>在堆内存中开辟一块空间，用来存储数组数据:new int[] { 1, 3, 5, 7 }</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/sRot61jZYW7n5Br.png" alt=""></p> <ul><li>在栈中开辟一块空间nums</li></ul> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/ob6hEn3rmzYuNgH.png" alt=""></em></p> <ul><li>把堆空间表示数组的内存地址赋给nums</li></ul> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/IGoZ912OWMLV7c4.png" alt=""></em></p> <p>这种把内存地址赋给一个变量，也被称之为引用关系（为了更清晰有人习惯画一根箭头来表示这种关系），也就是说nums变量引用了堆中某一块内存地址，<strong>当操作nums变量的时候，其实底层操作的是nums所引用内存地址里面的数据</strong>。</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/3Z2qOtQcf1iGVMP.png" alt=""></em></p> <p>所以此时，通过nums.length代码来查看nums数组中有几个元素，结果很明显是4。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>因为存在new，说明又会在堆空间开辟一块新的空间，赋初始值。</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/TP5JoGr7aHfOWLE.png" alt=""></strong></p> <ul><li>并把内存地址重新赋给nums变量，nums原来所引用的地址将被覆盖掉。</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/uICbcaPrXkwSQiK.png" alt=""></strong></p> <p>所以此时，通过nums.length代码来查看nums数组中有几个元素，结果很明显是3。</p> <p>注意：此时地址为0x1234的内存空间没有被任何变量所引用，所以该空间就变成了无用的垃圾，就等着垃圾回收器回收该空间。</p> <p>如果存在一行代码，如下：</p> <p>nums = null;</p> <p>null表示不再引用堆中的内存空间，那么此时nums就好比是没有初始化的，不能使用。</p> <h2 id="六、小结与作业"><a href="#六、小结与作业" class="header-anchor">#</a> 六、小结与作业</h2> <ul><li>数组的定义**-建议5遍**</li> <li>数组的初始化**-建议5遍**</li> <li>数组的基本操作和遍历操作**-建议5遍**</li> <li>数组综合应用练习**-建议5遍**</li> <li>JVM的内存模型和数组内存图**-建议5遍**</li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=Array" title="标签">#Array</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/67fd43/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">选择语句和循环语句</div></a> <a href="/pages/0840c0/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">方法定义与使用</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/67fd43/" class="prev">选择语句和循环语句</a></span> <span class="next"><a href="/pages/0840c0/">方法定义与使用</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/16.6e68160b.js" defer></script>
  </body>
</html>
